package com.example.firstassignment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ActivityCollection extends ListActivity {

	private List<String> activities = new ArrayList<String>();
	private Map<String,Class> name2class = new HashMap<String,Class>();
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		/* Add Activities to list */
        setup_activities();
        setListAdapter(new ArrayAdapter<String>(this, R.layout.main_list_item, activities));
        
        /* Attach list item listener */
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnActivityItemClick()); 
	}
	private class OnActivityItemClick implements OnItemClickListener {
    	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    		
    		/* Find selected activity */
    		String activity_name = activities.get(position);
    		Class activity_class = name2class.get(activity_name);

    		System.out.println("activity_name = "+activities.get(position));
    		System.out.println("activity_class = "+name2class.get(activity_name));
    		
    		/* Start new Activity */
    		Intent intent = new Intent(ActivityCollection.this,activity_class);
    		System.out.println("intent = "+intent);
    		ActivityCollection.this.startActivity(intent);
    	}   	
    }

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.activity_collection, menu);
//		return true;
//	}
	
    private void setup_activities() {
    	addActivity("Exercise 1 - Random",Random.class);
    	addActivity("Exercise 2 - BMI",BMI.class);
    	addActivity("Exercise 3 - ColorDisplay",ColorDisplay.class);
    	addActivity("Exercise 4 - MyCountries",MyCountries.class);  	
    	addActivity("Exercise 5 - VaxjoWeather",VaxjoWeather.class);  
    }
    
    private void addActivity(String name, Class activity) {
    	activities.add(name);
    	name2class.put(name, activity);    	
    }

}
